package com.example.farmer.Fragment.Circle;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.farmer.Adapter.PostCircleAdapter;
import com.example.farmer.Database.CirclePostDatabase;
import com.example.farmer.Entity.CirclePost;
import com.example.farmer.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class LatestFragment extends Fragment {
    private List<String> latestList;
    private RecyclerView recyclerView;
    private PostCircleAdapter postadapter;
    private TextView notLatest;
    private List<CirclePost> circlePosts;
    private CirclePostDatabase circlePostDatabase;
    private final MutableLiveData<List<CirclePost>> latestLiveData= new MutableLiveData<>();


    public static LatestFragment newInstance(List<String> latestList) {
        LatestFragment fragment = new LatestFragment();
        Bundle args = new Bundle();
        args.putStringArrayList("latestList", (ArrayList<String>) latestList);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            latestList = getArguments().getStringArrayList("latestList");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.latest_fragment, container, false);

        notLatest = view.findViewById(R.id.not_latest);

        recyclerView = view.findViewById(R.id.latest);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化数据库并获取所有帖子
        circlePostDatabase = CirclePostDatabase.getInstance(getContext());
        circlePosts = circlePostDatabase.getAllPosts();


        // 创建适配器并设置到RecyclerView
        postadapter = new PostCircleAdapter(circlePosts, getContext());
        recyclerView.setAdapter(postadapter);


        latestLiveData.observe(getViewLifecycleOwner(), this::updateUiWithPosts);
        refreshNewsData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment可见时刷新数据
        refreshNewsData();
    }
    private void refreshNewsData() {
        if (isAdded()) { // 确保 Fragment 已添加到 Activity
            // 从数据库获取最新数据
            circlePosts = circlePostDatabase.getAllPosts();
            // 检查 Fragment 是否还附加在 Activity 上
            if (isAdded()) {
                // 更新 LiveData 的值
                latestLiveData.setValue(new ArrayList<>(circlePosts));
            }
        }
    }
    private void updateUiWithPosts(List<CirclePost> circlePosts) {
        if (circlePosts != null && !circlePosts.isEmpty()) {
            postadapter.updatePosts(circlePosts);
            notLatest.setVisibility(View.GONE); // 假设没有数据时显示notNews
        } else {
            notLatest.setText("没有最新内容哦");
            notLatest.setVisibility(View.VISIBLE);
        }
    }
}

